package com.qf.controller;

import cn.hutool.crypto.SecureUtil;
import com.qf.service.UserService;
import com.qf.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
public class LoginController {

    @Autowired
    private UserService userService;

    @GetMapping("/login")
    public HashMap<String,Object> login(String username,String password){
        System.out.println(username);
        System.out.println(password);

        // 根据用户名去查询用户信息 （用户信息存放在一个文本文件中）
        String md5Password = userService.getMd5Password(username);
        // 比对密码
        boolean b = userService.comparePassword(password, md5Password);

        if (b){
            // 登录成功

            HashMap<String, Object> hashMap = new HashMap<>();

            hashMap.put("code",1);
            hashMap.put("msg","ok");

            return hashMap;
        }else {
            HashMap<String, Object> hashMap = new HashMap<>();

            hashMap.put("code",-1);
            hashMap.put("msg","登录失败");

            return hashMap;
        }


    }


    @GetMapping("/md5")
    public String md5(String password){
        // 把password进行md5运算，然后把结果返回
        String s = SecureUtil.md5(password);
        return s;
    }

}
